home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1995 June / MacFormat 25.iso / Shareware City / Developers / OutOfPhase1.1 Source / OutOfPhase Folder / GlobalWindowMenuList.c < prev    next >
Text File  |  1994-08-15  |  5KB  |  163 lines

  1. /* GlobalWindowMenuList.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "GlobalWindowMenuList.h"
  31. #include "Array.h"
  32. #include "Memory.h"
  33. #include "Menus.h"
  34.  
  35.  
  36. typedef struct
  37.     {
  38.         void*                                            Refcon;
  39.         void                                            (*RaiseCallback)(void* Refcon);
  40.         struct MenuItemType*            MenuItem;
  41.     } WindowMenuRec;
  42.  
  43.  
  44. static ArrayRec*                    MenuItemList = NIL;
  45.  
  46.  
  47. /* initialize the internal structures for the list */
  48. MyBoolean                    InitializeGlobalWindowMenuList(void)
  49.     {
  50.         ERROR(MenuItemList != NIL,PRERR(ForceAbort,
  51.             "InitializeGlobalWindowMenuList:  already initialized"));
  52.         MenuItemList = NewArray();
  53.         if (MenuItemList == NIL)
  54.             {
  55.              FailurePoint1:
  56.                 return False;
  57.             }
  58.         return True;
  59.     }
  60.  
  61.  
  62. /* dispose of the internal structures for the list */
  63. void                            ShutdownGlobalWindowMenuList(void)
  64.     {
  65.         ERROR(ArrayGetLength(MenuItemList) != 0,PRERR(ForceAbort,
  66.             "ShutdownGlobalWindowMenuList:  some menu items are still registered"));
  67.         DisposeArray(MenuItemList);
  68.         MenuItemList = NIL;
  69.     }
  70.  
  71.  
  72. /* add a new menu item to the window list */
  73. MyBoolean                    RegisterWindowMenuItem(struct MenuItemType* Item,
  74.                                         void (*RaiseFunction)(void* Refcon), void* Refcon)
  75.     {
  76.         WindowMenuRec*    Record;
  77.  
  78.         CheckPtrExistence(Item);
  79.         Record = (WindowMenuRec*)AllocPtrCanFail(sizeof(WindowMenuRec),"WindowMenuRec");
  80.         if (Record == NIL)
  81.             {
  82.              FailurePoint1:
  83.                 return False;
  84.             }
  85.         Record->Refcon = Refcon;
  86.         Record->RaiseCallback = RaiseFunction;
  87.         Record->MenuItem = Item;
  88.         if (!ArrayAppendElement(MenuItemList,Record))
  89.             {
  90.              FailurePoint2:
  91.                 ReleasePtr((char*)Record);
  92.                 goto FailurePoint1;
  93.             }
  94.         return True;
  95.     }
  96.  
  97.  
  98. /* remove a menu item from the window list */
  99. void                            DeregisterWindowMenuItem(struct MenuItemType* Item)
  100.     {
  101.         long                        Limit;
  102.         long                        Scan;
  103.  
  104.         CheckPtrExistence(Item);
  105.         Limit = ArrayGetLength(MenuItemList);
  106.         for (Scan = 0; Scan < Limit; Scan += 1)
  107.             {
  108.                 WindowMenuRec*    Record;
  109.  
  110.                 Record = (WindowMenuRec*)ArrayGetElement(MenuItemList,Scan);
  111.                 CheckPtrExistence(Record);
  112.                 if (Record->MenuItem == Item)
  113.                     {
  114.                         ArrayDeleteElement(MenuItemList,Scan);
  115.                         ReleasePtr((char*)Record);
  116.                         return;
  117.                     }
  118.             }
  119.         EXECUTE(PRERR(ForceAbort,"DeregisterWindowMenuItem:  item not found"));
  120.     }
  121.  
  122.  
  123. /* dispatch a window menu item.  If it wasn't one, then return False, otherwise */
  124. /* raise the appropriate window and return True */
  125. MyBoolean                    DispatchWindowMenuItem(struct MenuItemType* Item)
  126.     {
  127.         long                        Limit;
  128.         long                        Scan;
  129.  
  130.         CheckPtrExistence(Item);
  131.         Limit = ArrayGetLength(MenuItemList);
  132.         for (Scan = 0; Scan < Limit; Scan += 1)
  133.             {
  134.                 WindowMenuRec*    Record;
  135.  
  136.                 Record = (WindowMenuRec*)ArrayGetElement(MenuItemList,Scan);
  137.                 CheckPtrExistence(Record);
  138.                 if (Record->MenuItem == Item)
  139.                     {
  140.                         (*Record->RaiseCallback)(Record->Refcon);
  141.                         return True;
  142.                     }
  143.             }
  144.         return False;
  145.     }
  146.  
  147.  
  148. /* enable the items in the window menu */
  149. void                            WindowMenuEnableItems(void)
  150.     {
  151.         long                        Limit;
  152.         long                        Scan;
  153.  
  154.         Limit = ArrayGetLength(MenuItemList);
  155.         for (Scan = 0; Scan < Limit; Scan += 1)
  156.             {
  157.                 WindowMenuRec*    Record;
  158.  
  159.                 Record = (WindowMenuRec*)ArrayGetElement(MenuItemList,Scan);
  160.                 EnableMenuItem(Record->MenuItem);
  161.             }
  162.     }
  163.